package com.algorithm.stack;/*
 *@Author:DOWN
 *@Date:2021/12/14 17:54
 *@Comment:
 */

import com.algorithm.arrays.Arrays;

public class ArrayStack<E> implements StackInterface<E> {
    Arrays<E> arrays;

    public ArrayStack(int capacity) {
        arrays = new Arrays<>(capacity);
    }

    public ArrayStack() {
        arrays = new Arrays<>();
    }

    /**
     * 获取栈中元素个个数
     * @return int-size
     */
    @Override
    public int getSize() {
        return arrays.getSize();
    }

    /**
     * 获取栈的容量
     * @return int-Capacity
     */
    public int getCapacity() {
        return arrays.getCapacity();
    }

    /**
     * 栈中是否有元素
     * @return boolean-empty
     */
    @Override
    public boolean isEmpty() {
        return arrays.isEmpty();
    }

    /**
     * 入栈
     * @param item 元素
     */
    public void push(E item) {
        arrays.add(item);
    }

    /**
     * 出栈
     * @return 元素
     */
    @Override
    public E pop() {
        return arrays.removeLast();
    }

    /**
     * 查看栈顶
     * @return 元素
     */
    @Override
    public E peek() {
        return arrays.getLast();
    }

    @Override
    public String toString() {
        if (this.arrays == null) {
            return "null";
        }
        int iMax = this.arrays.getSize() - 1;
        if (iMax == -1) {
            return "[]";
        }
        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; i < this.arrays.getSize(); i++) {
            b.append(this.arrays.get(i));
            if (i == iMax) {
                return b.append(']').toString();
            }
            b.append(", ");
        }
        b.append("top");
        return b.toString();
    }
}
